/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tygzs.easygem.commandExecutor;

import java.util.List;
import net.tygzs.easygem.Main;
import net.tygzs.easygem.configuration.gem.RepairStoneConfig;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 *
 * @author thtTNT
 */
public class RepairStone implements CommandExecutor
{

    public boolean onCommand(CommandSender sender, Command cmd, String label, String args[])
    {
        if (args.length == 0)
        {
            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_RepairStone_1"));
            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_RepairStone_2"));
            return true;
        }
        if (args[0].equalsIgnoreCase("use"))
        {
            if (!sender.hasPermission("RepairStone.use"))
            {
                sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Error_noPermission"));
                return true;
            }
            Player player = (Player) sender;
            ItemStack item = player.getItemInHand();
            if (item == null)
            {
                sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("NothingInHand"));
                return true;
            }
            if (item.getType().getMaxDurability() == 0)
            {
                sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Can'tRepair"));
                return true;
            }
            ItemStack[] items = player.getInventory().getContents();
            for (int i = 0; i < items.length; i++)
            {
                ItemStack is = items[i];
                if (is == null)
                {
                    continue;
                }
                if (RepairStoneConfig.isRepairStone(is))
                {
                    int amount = is.getAmount();
                    RepairStoneConfig rs = new RepairStoneConfig(is);
                    if (amount == 1)
                    {
                        is.setType(Material.AIR);
                    }
                    is.setAmount(amount - 1);
                    items[i] = is;
                    player.getInventory().setContents(items);
                    if (Math.random() * 100 > rs.amount)
                    {
                        sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Info_RepairStone_use_fail"));
                        return true;
                    }
                    int NowDur = item.getDurability();
                    if (NowDur - rs.chance * item.getType().getMaxDurability() / 100 < 0)
                    {
                        item.setDurability(Short.valueOf("0"));
                    } else
                    {
                        item.setDurability((short) (NowDur - rs.chance * item.getType().getMaxDurability() / 100));
                    }
                    player.setItemInHand(item);
                    sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Info_RepairStone_use_success"));
                    return true;
                }
            }
        }
        Player player = (Player) sender;
        ItemStack item = player.getItemInHand();
        if (item.getType() == Material.AIR)
        {
            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("NothingInHand"));
            return true;
        }
        if (!RepairStoneConfig.isRepairStone(item))
        {
            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("NotRepairStone"));
            return true;
        }
        if (args[0].equalsIgnoreCase("chance"))
        {
            if (!sender.hasPermission("RepairStone.chance"))
            {
                sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Error_noPermission"));
                return true;
            }
            if (args.length == 1)
            {
                sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_RepairStone_chance"));
                return true;
            }
            if (args.length == 2)
            {
                try
                {
                    int chance = Integer.parseInt(args[1]);
                    List<String> lores = item.getItemMeta().getLore();
                    for (int i = 0; i < lores.size(); i++)
                    {
                        String lore = lores.get(i);
                        if (lore.startsWith((String) Main.plugin.getPluginConfig().getMessage().get("repair_chance")))
                        {
                            lores.set(i, Main.plugin.getPluginConfig().getMessage().get("repair_chance") + ":" + chance);
                            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Info_RepairStone_chance_set_true"));
                            return true;
                        }
                    }
                    sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Error_RepairStone"));
                    return true;
                } catch (NumberFormatException e)
                {
                    sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("EorrorNumber"));
                    return true;
                }
            }
            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_RepairStone_chance"));
            return true;
        }
        if (args[0].equalsIgnoreCase("repair"))
        {
            if (!sender.hasPermission("RepairStone.repair"))
            {
                sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Error_noPermission"));
                return true;
            }
            if (args.length == 1)
            {
                sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_RepairStone_repair"));
                return true;
            }
            if (args.length == 2)
            {
                try
                {
                    int chance = Integer.parseInt(args[1]);
                    List<String> lores = item.getItemMeta().getLore();
                    for (int i = 0; i < lores.size(); i++)
                    {
                        String lore = lores.get(i);
                        if (lore.startsWith((String) Main.plugin.getPluginConfig().getMessage().get("repair_repair")))
                        {
                            lores.set(i, Main.plugin.getPluginConfig().getMessage().get("repair_repair") + ":" + chance);
                            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Info_RepairStone_repair_set_true"));
                            return true;
                        }
                    }
                    sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Error_RepairStone"));
                    return true;
                } catch (NumberFormatException e)
                {
                    sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("EorrorNumber"));
                    return true;
                }
            }
            sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_RepairStone_repair"));
            return true;
        }
        sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_RepairStone_1"));
        sender.sendMessage((String) Main.plugin.getPluginConfig().getMessage().get("Help_RepairStone_2"));
        return true;
    }

}
